﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Exercises
{
    class DaoCustomer
    {
        public static void InsertCustomer(Customer cust)
        {
            DataClassNorthwindDataContext context = new DataClassNorthwindDataContext();
            using (context)
            {
                context.Customers.InsertOnSubmit(cust);
                context.SubmitChanges();
            }
        }

        public static void ModifyCustomer(Customer oldCustomer, Customer newCustomer)
        {
            DataClassNorthwindDataContext context = new DataClassNorthwindDataContext();
            using (context)
            {
                Customer customer =
                    (from curretnCust in context.Customers
                     where curretnCust.CustomerID == oldCustomer.CustomerID
                     select curretnCust).FirstOrDefault();
                if (customer == null)
                {
                    DaoCustomer.InsertCustomer(newCustomer);
                }
                else 
                {
                    if (oldCustomer.CustomerID == newCustomer.CustomerID)
                    {
                        customer.Address = newCustomer.Address;
                        customer.City = newCustomer.City;
                        customer.CompanyName = newCustomer.CompanyName;
                        customer.ContactName = newCustomer.ContactName;
                        customer.ContactTitle = newCustomer.ContactTitle;
                        customer.Country = newCustomer.Country;
                        customer.CustomerCustomerDemos = newCustomer.CustomerCustomerDemos;
                        customer.Fax = newCustomer.Fax;
                        customer.Orders = newCustomer.Orders;
                        customer.Phone = newCustomer.Phone;
                        customer.PostalCode = newCustomer.PostalCode;
                        customer.Region = newCustomer.Region;
                    }
                    else
                    {
                        context.Customers.DeleteOnSubmit(oldCustomer);
                        context.Customers.InsertOnSubmit(newCustomer);
                    }
                    context.SubmitChanges();
                }
            }
        }

        public static void DeleteCustomer(Customer cust)
        {
            DataClassNorthwindDataContext context = new DataClassNorthwindDataContext();
            using (context)
            {
                Customer customer =
                    (from curretnCust in context.Customers
                     where curretnCust.CustomerID == cust.CustomerID
                     select curretnCust).FirstOrDefault();               
                if (customer != null)
                {
                    context.Customers.DeleteOnSubmit(customer);
                }                
                context.SubmitChanges();
            }
        }
    }
}
